package com.example.springbootgraduation.tengxunapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.springbootgraduation.entity.ChinaTotal;
import com.example.springbootgraduation.entity.NocvData;
import com.example.springbootgraduation.service.ChinaTotalService;
import com.example.springbootgraduation.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//中国疫情实时数据
@Component
public class ChinaTotalScheduleTask {

    @Autowired
    private ChinaTotalService chinaTotalService;

    @Autowired
    private IndexService indexService;

    /**
     * 每小时执行一次，更新全国数据情况
     * @throws Exception
     */
    @Scheduled(fixedDelay = 3600000)//配置定时规则,3600S执行一次
//    @Scheduled(cron="0 0 8,9,10,11,12,13,18,20 * * ?")//每次到这个时间进行更新数据
    public void updateChinaTotalToDB() throws Exception{
        HttpUtils httpUtils = new HttpUtils();
        String string = httpUtils.getData();
        System.out.println("原始数据："+string);

        //1、所有数据的alibaba格式
        //拿到对象
        JSONObject jsonObject = JSONObject.parseObject(string);
        Object data = jsonObject.get("data");//获取到data数据
        System.out.println("data:"+data);

        //2、拿到data类型的alibaba
        JSONObject jsonObjectData = JSONObject.parseObject(data.toString());
        Object chinaTotal = jsonObjectData.get("chinaTotal");//拿到ChinaTotal的数据
        Object lastUpdateTime = jsonObjectData.get("overseaLastUpdateTime");//获得最新更新时间
        System.out.println("chinaTotal"+chinaTotal);

        //3、解析total 全中国整体疫情数据
        JSONObject jsonObjectTotal = JSONObject.parseObject(chinaTotal.toString());
        Object total = jsonObjectTotal.get("total");//获取到确诊人数
        System.out.println("total"+total);

        //4、全国数据total
        JSONObject totalData = JSONObject.parseObject(total.toString());
        Object confirm = totalData.get("confirm");//获取到确诊人数
        Object input = totalData.get("input");
        Object severe = totalData.get("severe");
        Object heal = totalData.get("heal");
        Object dead = totalData.get("dead");
        Object suspect = totalData.get("suspect");

        //5、为程序实体赋值
        ChinaTotal dataEntity = new ChinaTotal();
        dataEntity.setConfirm((Integer) confirm);
        dataEntity.setInput((Integer)input);
        dataEntity.setSevere((Integer)severe);
        dataEntity.setHeal((Integer)heal);
        dataEntity.setDead((Integer)dead);
        dataEntity.setSuspect((Integer)suspect);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dataEntity.setUpdateTime(format.parse(String.valueOf(lastUpdateTime)));

        //6、插入数据库
        chinaTotalService.save(dataEntity);

        /**
         * 中国各个省份的数据自动刷新
         */
        //拿到areaTree
        JSONArray areaTree = jsonObjectData.getJSONArray("areaTree");
        Object[] objects = areaTree.toArray();
        //遍历所有国家的数据
    /*    for (int i = 0; i < objects.length; i++) {
            JSONObject jsonObject1 = JSONObject.parseObject(objects[i].toString());
            Object name = jsonObject1.get("name");
            System.out.println(name);
        }*/

        //拿到中国的数据,数组第二条就是中国数据
        JSONObject jsonObject1 = JSONObject.parseObject(objects[2].toString());
        JSONArray children = jsonObject1.getJSONArray("children");//各个省份
        Object[] objects1 = children.toArray();//各个省份

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<NocvData> list = new ArrayList<>();//创建list
        for (int i = 0; i <objects1.length ; i++) {
            NocvData nocvData = new NocvData();//创建一个实体
            JSONObject jsonObject2 = JSONObject.parseObject(objects1[i].toString());
            Object name = jsonObject2.get("name");//省份名字
            Object lastUpdateTimeProvince = jsonObject2.get("lastUpdateTime");//省份更新数据时间
            Object total1 = jsonObject2.get("total");//拿到total
            JSONObject jsonObject3 = JSONObject.parseObject(total1.toString());//total得数据
            Object confirm1 = jsonObject3.get("confirm");//累计确诊数量

            //获取累积死亡人数 治愈人数数据
            Object heal1 = jsonObject3.get("heal");//累计治愈数量
            Object dead1 = jsonObject3.get("dead");//累计死亡数量

            //现存确诊人数
            int todayConfirm = Integer.parseInt(confirm1.toString())-Integer.parseInt(heal1.toString())-Integer.parseInt(dead1.toString());

//            System.out.println("省份->"+name+":"+confirm1+"人");

            //赋值
            nocvData.setName((name.toString()));
//            nocvData.setValue(Integer.parseInt(confirm1.toString()));
            nocvData.setValue(todayConfirm);//得到现存确诊人数
            //判断时间是否为空
            if (lastUpdateTimeProvince == null){
                nocvData.setUpdateTime(new Date());
            }else {
                nocvData.setUpdateTime(format1.parse(String.valueOf(lastUpdateTimeProvince)));
            }
            list.add(nocvData);
        }
        //各个省份得数据插入数据库（批量插入）
        indexService.saveBatch(list);
    }
}
